package com.logic.landseaserver.common.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;

import com.logic.landseaserver.ws.dto.MeterConsumptionDataDTO;

/**
 * Created by Junny on 2017/5/24.
 */
public class LandSeaUtil {
    /**
     * 根据systemcode得到租期 x月
     * @param monthType
     * @return
     */
   /* public static Integer getLease(String monthType){
        if(LeaseCodeEnum.LEAS1.getCode().equals(monthType)){
            return ConstantChar.one_num;
        }else if(LeaseCodeEnum.LEAS2.getCode().equals(monthType)){
            return ConstantChar.three_num;
        }else if(LeaseCodeEnum.LEAS3.getCode().equals(monthType)){
            return ConstantChar.six_num;
        }else if(LeaseCodeEnum.LEAS4.getCode().equals(monthType)){
            return ConstantChar.twelve_num;
        }else{
            return 0;
        }
    }*/
    /**
     * 根据付租方式得出要交多少个月房租、物业
     * @param payModel
     * @return
     */
  /*  public static Integer getHowManyMonthHourse(String payModel){
        if(PaymentModelCodeEnum.PAMO1.getCode().equals(payModel)
                || PaymentModelCodeEnum.PAMO8.getCode().equals(payModel)){
            return 1;
        }else if(PaymentModelCodeEnum.PAMO2.getCode().equals(payModel)){
            return ConstantChar.two_num;
        }else if(PaymentModelCodeEnum.PAMO3.getCode().equals(payModel)){
            return ConstantChar.three_num;
        }else if(PaymentModelCodeEnum.PAMO4.getCode().equals(payModel)){
            return ConstantChar.two_num;
        }else if(PaymentModelCodeEnum.PAMO5.getCode().equals(payModel)){
            return ConstantChar.three_num;
        }else if(PaymentModelCodeEnum.PAMO6.getCode().equals(payModel)){
            return ConstantChar.six_num;
        }*//*else if(PaymentModelCodeEnum.PAMO7.getCode().equals(payModel)){
            return 1;
        }*//*else{
            return 0;
        }
    }*/

    /**
     * 根据付租方式得出要交多少个月押金
     * @param payModel
     * @return
     */
  /*  public static Integer getHowManyMonthDeposit(String payModel){
        if(PaymentModelCodeEnum.PAMO1.getCode().equals(payModel)){
            return ConstantChar.two_num;
        }else if(PaymentModelCodeEnum.PAMO2.getCode().equals(payModel)){
            return ConstantChar.two_num;
        }else if(PaymentModelCodeEnum.PAMO3.getCode().equals(payModel)){
            return ConstantChar.two_num;
        }else if(PaymentModelCodeEnum.PAMO4.getCode().equals(payModel)){
            return 1;
        }else if(PaymentModelCodeEnum.PAMO5.getCode().equals(payModel)){
            return 1;
        }else if(PaymentModelCodeEnum.PAMO6.getCode().equals(payModel)){
            return 1;
        }else if(PaymentModelCodeEnum.PAMO7.getCode().equals(payModel)){
            return 1;
        }else if(PaymentModelCodeEnum.PAMO8.getCode().equals(payModel)){
            return 1;
        }else{
            return 0;
        }
    }*/

    /**
     * 根据当前账单期数，付租方式,租期计算出当月房租和物业账单应缴纳X月
     * @param nowTerm 当前账单期数
     * @param payMonth 付X
     * @param leaseMonth 租期
     * @return
     */
    public static Integer getPayMonth(int nowTerm,int payMonth,int leaseMonth){
        for (int i = 1; i < leaseMonth; i++) {
            if (i % payMonth == 0) {
                if (nowTerm == i + 1) {
                    return payMonth;
                }
            }
        }
        return 0;
    }

    /**
     * 四舍五入保留两位小数
     * @param a
     * @return
     */
    public static Double get2Double(Double a){
        if (null == a)
        {
            a = new Double(0.00);
        }
        DecimalFormat df = new DecimalFormat("0.00");
        return new Double(df.format(a).toString());
    }
    
    
    /**
     * add by crystal
     * 根据房间编号查询房间用量
     * @param roomId
     * @param mdList
     * @return 房间用量
     */
    public static MeterConsumptionDataDTO queryUsedByRoomId(Integer roomId, List<MeterConsumptionDataDTO> mdList) {
        MeterConsumptionDataDTO mdInfo = null;
        try{
            if (mdList.size() > 0) {
                for (int i = 0; i < mdList.size(); i++) {
                    if (roomId==mdInfo.getRoomId()) {
                    	mdInfo  = mdList.get(i);
                    }
                }
            }
        }catch (Exception e) {
            return mdInfo;
        }
        return mdInfo;
    }


    /**
     * 获取真实的支付方式
     * @param payMode 支付方式
     * @param leaseNum 租期
     * @return
     */
  /*  public static String getRealPayMode(String payMode,int leaseNum){
        String realMode=payMode;
        if(leaseNum==1 && (payMode.equals(PaymentModelCodeEnum.PAMO1.getCode()) ||
                payMode.equals(PaymentModelCodeEnum.PAMO8.getCode()))){
            realMode= PaymentModelCodeEnum.PAMO7.getCode();
        }
        if(leaseNum==ConstantChar.three_num &&  (payMode.equals(PaymentModelCodeEnum.PAMO3.getCode())
                ||  payMode.equals(PaymentModelCodeEnum.PAMO5.getCode()))){
            realMode= PaymentModelCodeEnum.PAMO7.getCode();
        }
        if(leaseNum==ConstantChar.six_num &&  payMode.equals(PaymentModelCodeEnum.PAMO6.getCode())){
            realMode= PaymentModelCodeEnum.PAMO7.getCode();
        }
        return realMode;
    }*/

    /**
     * 
     * [简要描述]：根据monthlyBill中的billmonth获取Bill表中的</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-09-20)</br>
     *
     * @param monthlyBillMonth
     * @return
     * @throws ParseException
     *
     */
    public static String getBillMonth(String monthlyBillMonth) throws ParseException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date month = sdf.parse(monthlyBillMonth);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.add(Calendar.MONTH, 1);
        String lastBillMonth = sdf.format(calendar.getTime());
        return lastBillMonth;
    }
    
    /**
     * 
     * [简要描述]：根据Bill表billMonth获取monthlybill表中的BillMonth</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-09-20)</br>
     *
     * @param billMonth
     * @return
     * @throws ParseException
     *
     */
    public static String getMonthlyBillMonth(String billMonth) throws ParseException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date month = sdf.parse(billMonth);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.add(Calendar.MONTH, -1);
        String lastBillMonth = sdf.format(calendar.getTime());
        return lastBillMonth;
    }

    /**
     * 四舍五入取整
     * @param a
     * @return
     */
    public static Double getIntDouble(Double a){
        if (null == a)
        {
            a = new Double(0.00);
        }
        DecimalFormat df = new DecimalFormat("0.00");
        return new BigDecimal(a).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static <T extends Comparable<T>> boolean compare(List<T> a, List<T> b) {
        if (a.size() != b.size())
            return false;
        Collections.sort(a);
        Collections.sort(b);
        for (int i = 0; i < a.size(); i++) {
            if (!a.get(i).equals(b.get(i)))
                return false;
        }
        return true;
    }

    /**
     * 获取账单名中的多少期账单
     * @return
     */
    public static Integer getNumberFromStr(String strs){
        String regEx="[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(strs);
        return  Integer.parseInt(m.replaceAll("").trim());
    }
/*
    public static void main(String arg[]){
       //Integer payM =   LandSeaUtil.getPayMonth2(12,);
        //当前账单期数  付X  租期
        Double intA =   LandSeaUtil.getIntDouble(9.45);
        System.out.println(intA);
         Double a = 100.00;
         Double b = 1000.00;
         System.out.println(a.compareTo(b)>0);
        System.out.println(getNumberFromStr("第12账单"));
    }*/
    
    /**
     * 
     * [简要描述]：获得两个list的差异</br>
     * [详细描述]：将相同的从AB集合中剔除</br>
     * [作者]：Aaron(2017-12-22)</br>
     *
     * @param listA
     * @param listB
     *
     */
    public static void getStrListDiffrent(List<String> listA, List<String> listB)
    {
        if (CollectionUtils.isEmpty(listA) || CollectionUtils.isEmpty(listB))
        {
            return;
        }
        
        Iterator<String> iteratorA = listA.iterator();
        while (iteratorA.hasNext())
        {
            String str = iteratorA.next();
            if (listB.contains(str))
            {
                iteratorA.remove();
                listB.remove(str);
            }
        }
    }
}
